package org.example.doctorapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.doctorapi.data.SysUser;
import org.example.doctorapi.mapper.SysUserMapper;
import org.example.doctorcommon.util.BaseResult;
import org.example.doctorcommon.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/usr")
@RestController
public class UserLoingController {
    @Autowired
    private PasswordEncoder encoder;
    @Autowired
    private SysUserMapper sysUserMapper;
    @GetMapping("/login")
    public BaseResult login(@RequestParam("username") String username
            , @RequestParam("password") String password){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name",username);
        SysUser user = sysUserMapper.selectOne(queryWrapper);
        boolean matches = encoder.matches(password, user.getPassword());
        if (!matches) return BaseResult.ok("密码错误");
        String token = JwtUtils.getToken(user.getUserName(),user.getUserId());
        return BaseResult.ok(token);
    }

    @GetMapping("/login1")
    public BaseResult login1(){
        return BaseResult.ok("11");
    }
}
